var GroupDetail = {
    init: function(){
        RegNotification("groupNameSet", function(json) {
            if (json.action == "edit") {
                var group = GetLS("alist_gdetail");
                $("#name").text(group.name);
                $("#userNum").text(group.friends.length);
            }
        });
        var group = GetLS("alist_gdetail");
        $("#name").text(group.name);
        $("#userNum").text(group.friends.length);
        $("#photo").css("background-image", "url('"+ Core.id2url({"type":Role.NORMAL}) +"')");
    },
    removeGroup: function(){
        uexWindow.confirm(CurrentUser.category==30?"警告":"Warning",CurrentUser.category==30?"确定要删除吗？":"Do you want to detete?",CurrentUser.category==30?"确定,取消":"Sure,Cancel");
        function cbConfirm(opId, dataType, ensure){
            if(ensure == 0){
                var group = GetLS("alist_gdetail");
                console.log(group)
                var data = {};
                data.from = group.name;
                $ajax({
                    url: "/user/removeGroupName",
                    data: data,
                    success: function(res) {
                        PCall("address","CCall('Alist.init()')");
                        WinClose();
                    }
                });
            }
        }
        uexWindow.cbConfirm = cbConfirm;     
    },
    toUpdateGroup: function (){
        OpenNewWin("group_edit");
    },
    toManageGroup: function(){
        OpenNewWin("group_manage");
    },
    isOptimize: true,
    chooseUser: new Map(), /**所有的用户,map结构*/
    bUserIds: new Array(), /**当前组内的用户*/
    cUserIds: new Array(), /**选择后的用户id*/
    rUserIds: new Array(), /**相对于当前用户删除的id*/
    aUserIds: new Array(), /**相对于当期用户添加的id*/
    initManage: function(){
        var lv1 = appcan.listview({
            selector : "#ctn",
            type : "thinLine",
            hasIcon : false,
            hasAngle : false,
            hasSubTitle : true,
            multiLine : 1,
            hasCheckbox : false,
            align : 'left'
        });
        
        lv1.set(GroupDetail.myInitList());
        
        lv1.on('click',function(Obj,Data,subObj){
            var lTarget = $(Obj[0]).children().children().children()[0];
            var mobj = $(lTarget);
            var _id = mobj.attr('id');
            var flag = (GroupDetail.bUserIds.indexOf(_id) == -1); /**所选id不在当前组内*/
            if (!mobj.hasClass("im_checkbox_selected")){  /**添加*/
                if(GroupDetail.cUserIds.indexOf(_id) == -1){ /**选择后用户*/
                    GroupDetail.cUserIds.push(_id);
                }
                if(flag && GroupDetail.aUserIds.indexOf(_id) == -1){ /**添加用户*/
                    GroupDetail.aUserIds.push(_id);   
                }
                if(!flag && GroupDetail.aUserIds.indexOf(_id) != -1){
                    GroupDetail.rUserIds.mremove(_id);
                }
                mobj.addClass("im_checkbox_selected");
            }else{ /**移除*/
                console.log(GroupDetail.cUserIds)
                if(GroupDetail.cUserIds.indexOf(_id) != -1){ /**选择后用户*/
                    GroupDetail.cUserIds.mremove(_id);
                }
                if(flag && GroupDetail.aUserIds.indexOf(_id) != -1){
                    GroupDetail.aUserIds.mremove(_id);   
                }
                if(!flag && GroupDetail.rUserIds.indexOf(_id) == -1){ /**删除用户*/
                    GroupDetail.rUserIds.push(_id);
                }
                mobj.removeClass("im_checkbox_selected");
            }
        });
        
        // 原始ui改为自定义ui
        var rEles = $("#ctn").children()[0];
        var eles = $(rEles).children();
        for(var i = 0;i < eles.length;i++){
            var ele = eles[i];
            if($(ele).hasClass("ubb")) $(ele).removeClass("ubb");
            if($(ele).hasClass("lis")) $(ele).removeClass("lis");
            if(!$(ele).hasClass("my_select_li")) $(ele).addClass("my_select_li");
            //$(ele).css({"style":"padding: 0.1em 0.5em;min-height:2em;"});
        }
    },
    myInitList: function(){ // 列表数据初始化
        var nIds = new Array();
        var group = GetLS("alist_gdetail");
        $("#name").text(group.name);
        var users = group.friends;
        var html = "";
        var myList = new Array();
        var chooseUser = GroupDetail.chooseUser;
        
        for(var i = 0 ;i<users.length;++i){
             nIds.push(users[i]._id);
             chooseUser.put(users[i]._id,users[i]);
             var ele =  '<div class="umh4 umw3 ub ub-ac">'+
                    '<div class="uw1p5 uh1p5 ub_img im_checkbox im_checkbox_selected" id="'+users[i].id+'"></div><div class="uml_p5 ub ub-ac">'+users[i].name+'</div>'+
                  '</div>';
              html += ele;
              myList.push({title:ele});
              GroupDetail.bUserIds.push(users[i]._id);
        };
        GroupDetail.cUserIds = GroupDetail.bUserIds;
        
        var allGroup = GetLS("alist_groups");
        for(var k = 0;k < allGroup.length;k++){
            var iiUsers = allGroup[k].friends;
            for(var m = 0;m < iiUsers.length;m++){
                 if(nIds.indexOf(iiUsers[m]._id) == -1){
                     chooseUser.put(iiUsers[m]._id,iiUsers[m]);
                     var ele =  '<div class="umh4 umw3 ub ub-ac">'+
                    '<div class="uw1p5 uh1p5 ub_img im_checkbox" id="'+iiUsers[m].id+'"></div><div class="uml_p5 ub ub-ac">'+iiUsers[m].name+'</div>'+
                  '</div>';
                  html += ele;
               myList.push({title:ele});
                 }
            }
        }
        GroupDetail.chooseUser = chooseUser;
        //SetLS("chooseUser",chooseUser);
        return myList;
        //$("#ctn").html(html);  
    },
    postManage: function(){
        var data = {};
        // 数据处理
        var group = GetLS("alist_gdetail");
        var userIds = GroupDetail.cUserIds; /**选择后的用户ids*/
        var chooseUser = GroupDetail.chooseUser; /** 所有的用户*/
        console.log(GroupDetail.aUserIds);

        // 数据赋值
        data.group_name = group.name;
        data.adds = GroupDetail.aUserIds;
        data.removes = GroupDetail.rUserIds;
        // 数据提交 GroupDetail.sendAjax
        $ajax({
            url: "/user/manageGroup",
            data: data,
            success: function(res) {
                PCall("address","CCall('Alist.init()')");
                WinClose();
            }
        });
    },
    updateCurrentUserGroup: function(){
        var data = {id:CurrentUser._id};
        $ajax({
            url: "/users/getUserInfo",
            data: data,
            success: function(res) {
                if(res){
                    var user = res.result[0];
                    if(user){
                        SetLS("user", user);
                        CurrentUser = user;
                    }
                }
            }
        });
    }
};
